<template>
    <view style="height: 100%">
      <scroll-view :scroll-y="true" :scroll-x="false" class="scroll" @scrolltolower="bindlower">
        <view class="swiper">
            <block v-for="(item, index) in custList" :key="index">
                <!-- parse <template is="mydating" :data="item"/> -->
                <block name="mydating">
                    <view class="item" @tap="toDetail" :id="item.materialId">
                        <view class="ddhead">
                            <text class="ddhao">{{item.materialName}}</text>
                            <text class="state">
                                <block>{{ item.username }}</block>
                            </text>
                        </view>                        
                        <view class="ddfooter">
                            <view>
                                <text class="state">
                                    <block>{{item.createTime}} {{item.teacherName}}</block>
                                </text>
                                <view class="btn cancel" :id="item.id" @tap="cancelDating">删除</view>
                            </view>
                        </view>
                    </view>
                </block>
            </block>
            <view class="prompt" v-if="custList.length == 0">
                <image src="/static/images/dd.png" />
                <text>您没有任何浏览的资料</text>
            </view>
        </view>
        <!-- template对应的原始代码，为保证正常显示，已对其进行隐藏。 -->
        <block name="mydating" v-if="false">
            <view class="item">
                <view class="ddhead">
                    <text class="ddhao">{{item.studentName}}-{{ item.classesName }}【{{item.teacherNameJw}} {{item.teacherNameJx}}】</text>
                    <text class="state">
                      <block>¥{{ item.consumeAmount }}</block>
                    </text>
                </view>                
                <view class="ddfooter">
                    <view>
                        <text class="state">
                            <block>{{item.createTime}} {{item.teacherName}}</block>
                        </text>
                        <view class="btn cancel" :id="item.id" @tap="cancelDating">删除</view>
                    </view>
                </view>
            </view>
        </block>
      </scroll-view>  
    </view>
</template>

<script>
var config = require('../../config.js');
var http = require('../../utils/httpHelper.js');
export default {
    data() {
        return {
            userInfo: {},
            custList: [],
            timer: {},
            datingPage: {
                current: 1,
                pageSize: 10,
                total: 0
            }
        };
    },
    onLoad: function () {
        var that = this;
        let userInfo = uni.getStorageSync('user');
        that.setData({
            userInfo: userInfo
        });
        that.datingPage.current = 1;
    },
    onShow: function () {
        var that = this;
        that.datingPage.current = 1;
        this.getlist(function (list) {
            that.setData({
                custList: list
            });
        });
    },
    methods: {
        getMoreData() {
            var that = this;
            that.datingPage.current++;
            that.getlist(function (list) {
                let yList = that.custList;
                yList = yList.concat(list);
                that.setData({
                    custList: yList
                });
            });
        },

        getlist: function (callback) {
            var that = this;
            http.httpPost(
                config.clubApi.getViewList,
                {
                    pageNo:that.datingPage.current,
                    pageSize:that.datingPage.pageSize,
                    userId:that.userInfo.userId,
                    appid: config.APPID
                },
                function (res) {
                    if (res.code == '200') {
                        var list = res.data.pageData;
                        if (typeof callback == 'function') {
                            callback(list);
                        }
                    }
                }
            );
        },

        cancelDating: function (e) {
            var that = this;
            http.httpPost(
                config.clubApi.delViewzl,
                {
                    id: e.target.id
                },
                function (res) {
                    if (res.code == '200') {
                        uni.showToast({
                            title: '删除成功!',
                            icon: 'success',
                            duration: 1000
                        });
                        that.getlist(function (list) {
                            that.setData({
                                custList: list
                            });
                        });
                    }
                }
            );
        },

        //处理分页
        bindlower: function (e) {
          if(this.timer != null){
            clearTimeout(this.timer)
          }
          this.timer = setTimeout(()=>{
            this.getMoreData();
          },500)
        },
        toDetail: function (e) {
            var id = e.currentTarget.id;
            let userInfo = uni.getStorageSync('user');
            if (userInfo) {
              uni.navigateTo({
                  url: '../material/detail/index?id=' + id +'&inviteId=' + userInfo.userId
              });
            }else {
              uni.navigateTo({
                url: '../material/detail/index?id=' + id
              });
            }
        }
    }
};
</script>
<style lang="scss">
@import './index.scss';
@import '@/pages/dating/index.scss';
</style>
